package com.systop.dxjj.modules.recruitinfo.service.impl;

import com.alibaba.fastjson.JSON;
import com.systop.dxjj.common.pojo.*;
import com.systop.dxjj.common.utils.JwtUtil;
import com.systop.dxjj.common.utils.R;
import com.systop.dxjj.common.utils.RedisCache;
import com.systop.dxjj.modules.recruitinfo.mapper.RecruitinfoMapper;
import com.systop.dxjj.modules.recruitinfo.service.RecruitinfoService;
import com.systop.dxjj.modules.teacher.mapper.TeacherMapper;
import com.systop.dxjj.modules.teacher.service.impl.TeacherServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class RecruitinfoServiceImpl implements RecruitinfoService {

    @Autowired
    RecruitinfoMapper recruitinfoMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    private RedisCache redisCache;
    TeacherUser teacherUser;
    @Override
    public R getAll(){
        List<RecruitinfoVO> allRecruitinfo = recruitinfoMapper.getAllRecruitinfo();
        return R.ok().put("allRecruitinfo",allRecruitinfo);
    }

    @Override
    public R getById(Integer teachid){
        List<RecruitinfoVO> recruitinfoById = recruitinfoMapper.getRecruitinfoById(teachid);
        return R.ok().put("recruitinfoById",recruitinfoById);
    }

    @Override
    public R getByName(String teachname) {
        RecruitinfoVO recruitinfoByName = recruitinfoMapper.getRecruitinfoByName(teachname);
        return R.ok().put("recruitinfoByName",recruitinfoByName);
    }

    @Override
    public R deleteById(Integer recruitid){
        int s = recruitinfoMapper.deleteRecruitinfoById(recruitid);
        if(s>0){
            return R.ok().put("msg","删除成功");
        }else {
            return R.ok().put("msg","删除失败");
        }
    }

    @Override
    public R addRecruitinfo(String turtortype, String pay, String location, String classtype, String detail, Integer teachid, HttpServletRequest request){
        if(StringUtils.isEmpty(turtortype) || StringUtils.isEmpty(pay) || StringUtils.isEmpty(location) ||
                StringUtils.isEmpty(classtype) || StringUtils.isEmpty(detail) || StringUtils.isEmpty(String.valueOf(teachid))){
            return R.ok().put("start",0);
        }else {
            Recruitinfo recruitinfo = new Recruitinfo();
            recruitinfo.setTurtortype(turtortype);
            recruitinfo.setPay(pay);
            recruitinfo.setLocation(location);
            recruitinfo.setClasstype(classtype);
            recruitinfo.setDetail(detail);
            //获取登录用户id
            String token = request.getHeader("token");
            String userid;
            try {
                Claims claims = JwtUtil.parseJWT(token);
                userid = claims.getSubject();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("token非法");
            }
            String tearedisKey = "login:" + userid;
            Object cacheObject = redisCache.getCacheObject(tearedisKey);
            if (cacheObject instanceof StudentUser) {
                teacherUser = (TeacherUser) cacheObject;
            } else {
                teacherUser = JSON.parseObject(JSON.toJSON(cacheObject).toString(), TeacherUser.class);
            }
            Integer teachid1 = teacherUser.getTeacher().getTeachid();
            recruitinfo.setTeachid(teachid1);
            //传入时间
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            recruitinfo.setPublishtime(timestamp);
            recruitinfo.setRecruitstatus(1);
            int r = recruitinfoMapper.addRecruitinfo(recruitinfo);
            if (r == 1){
                return R.ok().put("start",1);
            }else {
                return R.ok().put("start",0);
            }
        }

    }

    @Override
    public Integer getTeachidByRecruitid(Integer recruitid) {
        return recruitinfoMapper.getTeachidByRecruitid(recruitid);
    }
}
